<template>
     <DeviceTreeWithSpares /> 
  </template>
  
  <script setup>
  import { ref } from 'vue'
  import { listSbtz } from '@/api/sbtz/sbtz'
  import DeviceListWithSpares from '@/components/sbtz/node/DeviceListWithSpares.vue'
  import DeviceTreeWithSpares from '@/components/sbtz/node/DeviceTreeWithSpares.vue'
  
  
  
  const loading = ref(false)
  const treeRows = ref([])
  
  function typeLabel(t) {
    return { SYSTEM:'系统', MACHINE:'母设', MODULE:'子设', COMPONENT:'部件', PART:'零件' }[t] || t
  }
  function typeColor(t) {
    return { SYSTEM:'success', MACHINE:'primary', MODULE:'info', COMPONENT:'warning', PART:'danger' }[t] || ''
  }
  
  async function onSearch(params) {
    loading.value = true
    const { rows = [] } = await listSbtz({ ...params, pageNum: 1, pageSize: 1000 })
    // 转树结构
    const map = new Map()
    rows.forEach(r => map.set(r.id, { ...r, children: [] }))
    const roots = []
    rows.forEach(r => {
      const pid = r.parentId || 0
      if (pid === 0) roots.push(map.get(r.id))
      else map.get(pid)?.children.push(map.get(r.id))
    })
    treeRows.value = roots
    loading.value = false
  }
  </script>
  